package com.weiqiao.api.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.weiqiao.api.domain.AppUserApiEntity;
import com.weiqiao.api.domain.BankApiEntity;
import com.weiqiao.api.domain.CardCommodityApiEntity;
import com.weiqiao.api.domain.TeamCommodityApiEntity;
import com.weiqiao.api.domain.vo.BankApiVO;
import com.weiqiao.api.repository.IAppuserApiRepository;
import com.weiqiao.api.repository.IBankApiRepository;
import com.weiqiao.api.repository.ICardCommodityApiRepository;
import com.weiqiao.api.repository.ITeamCommodityApiRepository;
import com.weiqiao.api.service.IBankApiService;


import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;


@Service("BankApiServiceImpl")
public class BankApiServiceImpl implements IBankApiService {

    @Autowired
    private IBankApiRepository bank;

    @Autowired
    private ICardCommodityApiRepository commodity;

    @Autowired
    private IAppuserApiRepository appuserApiRepository;

    @Autowired
    private ITeamCommodityApiRepository teamCommodityApiRepository;



    @Override
    public List<BankApiEntity> hotBankList(BankApiVO vo) {
        AppUserApiEntity entity = new AppUserApiEntity();
        if (vo.getUser_id() > 0) {
            // 获取用户信息
            String info = appuserApiRepository.getAppUserByFilter(vo.getCompany_id(), new Document("user_id", vo.getUser_id()));
            if (!StringUtil.isBlank(info)) {
                entity = new Gson().fromJson(info, AppUserApiEntity.class);
            }
        }
        Document doc = new Document(); //银行查询条件
        doc.append("delete_flag", 0);
        int company_id = vo.getCompany_id(); // 公司id
        if (!StringUtil.isBlank(vo.getBank_id())) {
            doc.append("bank_id", vo.getBank_id());
        }
        Document sort = new Document();
        sort.append("bank_sort", 1);
        int pagesize = vo.getPagesize();
        int pagenum = vo.getPagenum() * pagesize;
        List<String> list = bank.hotBankList(doc, sort, pagenum, pagesize, company_id);
        List<BankApiEntity> result = new ArrayList<>();
        Gson gson = new Gson();
        for (String s : list) {
            BankApiEntity bankApiEntity = gson.fromJson(s, BankApiEntity.class);
            // 1.根据银行id进行查询，就是查询单个银行信息，不用计算佣金。不在继续往下执行，返回
            // 2. 客户是没有佣金的.不在继续往下执行，返回
            if (!StringUtil.isBlank(vo.getBank_id()) || entity.getMember_type() == 0) {
                result.add(bankApiEntity);
                continue;
            }
            // 通过当前用户不同的角色，查询每个银行若下单购卡，可得最高佣金
            bankApiEntity.setCommission(this.getCommission(vo.getCompany_id(), entity, bankApiEntity));
            result.add(bankApiEntity);
        }
        return result;
    }


    /**
     * 计算每个银行的当前用户可得到的最高佣金
     * @param companyId
     * @param user
     * @param bank
     * @return
     */
    private double getCommission(int companyId, AppUserApiEntity user, BankApiEntity bank) {
        double commission = 0;
        // 分销员，若分销员没有团队，不在继续向下执行
        if (user.getMember_type() == 1 && !StringUtil.isBlank(user.getTeam_id() )) {
            // 找到当前银行当前团队，分销员最高的佣金
            Bson filter = new Document("bank_id", bank.getBank_id()).append("team_id", user.getTeam_id());
            // 排序字段，团队佣金取最大值
            Bson sort = new Document("distribution_total", -1);
            String info = teamCommodityApiRepository.findMaxCommissionTeamCommodity(companyId, filter, sort);
            if (StringUtil.isBlank(info)) return commission;
            TeamCommodityApiEntity teamCommodityApiEntity = new Gson().fromJson(info, TeamCommodityApiEntity.class);
            return teamCommodityApiEntity.getDistribution_total();
        }
        // 团长，若团长没有团队id，也不在向下执行
        if (user.getMember_type() == 2 && !StringUtil.isBlank(user.getTeam_id())) {
            // 找到当前银行当前团队，最高的佣金
            Bson filter = new Document("bank_id", bank.getBank_id()).append("team_id", user.getTeam_id());
            // 排序字段，团队佣金取最大值
            Bson sort = new Document("team_total", -1);
            String info = teamCommodityApiRepository.findMaxCommissionTeamCommodity(companyId, filter, sort);
            if (StringUtil.isBlank(info)) return commission;
            TeamCommodityApiEntity teamCommodityApiEntity = new Gson().fromJson(info, TeamCommodityApiEntity.class);
            return teamCommodityApiEntity.getTeam_total();
        }
        // 省长
        if (user.getMember_type() == 3) {
            // 找到当前银行，当前省长，最高可拿金额
            Bson filter = new Document("bank_id", bank.getBank_id()).append("operator_list.user_id", user.getUser_id());
            // 排序字段，团队佣金取最大值
            Bson sort = new Document("commission", -1);
            String info = commodity.findMaxCommissionCommodity(companyId, filter, sort);
            if (StringUtil.isBlank(info)) return commission;
            CardCommodityApiEntity teamCommodityApiEntity = new Gson().fromJson(info, CardCommodityApiEntity.class);
            return teamCommodityApiEntity.getCommission();
        }
        return commission;
    }

    @Override
    public List<BankApiEntity> hotBankDetail(BankApiVO vo) {
        Document doc = new Document(); //银行查询条件
        doc.append("delete_flag", 0);
        doc.append("bank_id", vo.getBank_id());
        int company_id = vo.getCompany_id();
        String bank_list = bank.hotBankDetail(company_id, doc);
        List<BankApiEntity> bank_result = new ArrayList<>();
        Gson gson = new Gson();
        BankApiEntity dto = gson.fromJson(bank_list, BankApiEntity.class);
        bank_result.add(dto);
        return bank_result;
    }

}
